package app.system;

import javax.swing.JFrame;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JList;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTree;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.File;

import java.util.*;
import java.awt.Component;
import java.net.*;
import javax.swing.tree.*;
import java.awt.Color;
import java.awt.GridLayout;


public class SystemFrame extends JFrame {

	
    private JPanel contentPane = (JPanel) this.getContentPane();
    private JTabbedPane tabs = new JTabbedPane();
    
    //menus
    private JMenuBar mainMenuBar;
    private JMenu fileMenu, helpMenu, kbMenu, queryMenu, fileMenuLoad;
    private JMenuItem fileMenuExit, fileMenuLoadKB, fileMenuLoadQuery, helpMenuAbout, helpMenuConfig, helpMenuSyntax,
    	kbMenuReasoner, kbMenuSat, queryMenuCreate, queryMenuEval;
    //
    
    private JList jListClasses = new JList();
    private JScrollPane jScrollPane1 = new JScrollPane();
    private JLabel jLabel1 = new JLabel();
    private JButton jButton1 = new JButton();
    private JScrollPane jScrollPane2 = new JScrollPane();
    //private JLabel jLabel2 = new JLabel();
    //private DefaultListModel jList1Model = new DefaultListModel();
    private JEditorPane planPane=new JTextPane();
    private JScrollPane planScroll = new JScrollPane();
    private JEditorPane gciPane=new JTextPane();
    private JScrollPane gciScroll = new JScrollPane();

    //labels 
    private final String frameTitle = "Object Retrieval System for Knowledge-based Data";
    private final String sfile = "File";
    private final String sfileload = "Load";
    private final String sfileloadkb = "Load KB/Data";
    private final String sfileloadquery = "Load Queries";
	private final String sexit = "Exit";
	private final String sabout = "About The System";
	private final String shelp = "Help";
	private final String shelpconf = "Options";
	private final String shelpsyntax = "Syntax Guide";
	private final String skb = "Knowledge Base";
	private final String skbsat = "Consistency Check";
	private final String skbresoner = "Set Default Reasoner";
	private final String squery = "Query";
	private final String squerycreate = "Issue Queries";
	private final String squeryeval = "Evaluate";
	//
	private final String syntaxFilePath = "SyntaxGuide.html";
    private final String syntaxFileTitle = "Syntax References for KB and Queries";
	
	
    public SystemFrame() {
        this.setTitle(this.frameTitle);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void initMenus(){
    	//ImageIcon defaulticon = new ImageIcon("images/select.gif");
    	ImageIcon kbfile = new ImageIcon("images/kb.gif");
    	ImageIcon qfile = new ImageIcon("images/query.gif");
    	ImageIcon about = new ImageIcon("images/about.gif");
    	ImageIcon config = new ImageIcon("images/config.gif");
    	ImageIcon sat = new ImageIcon("images/sat.gif");
    	ImageIcon eval = new ImageIcon("images/eval.gif");
    	ImageIcon reasoner = new ImageIcon("images/reasoner.gif");
    	ImageIcon syntaxguide = new ImageIcon("images/syntaxguide.gif");
    	ImageIcon exiticon = new ImageIcon("images/exit.gif");
    	ImageIcon writequery = new ImageIcon("images/writequery.gif");
    	mainMenuBar = new JMenuBar();
    	this.setJMenuBar(mainMenuBar);
    	fileMenu = new JMenu(this.sfile);
    	kbMenu = new JMenu(this.skb);
    	queryMenu = new JMenu(this.squery);
    	helpMenu = new JMenu(this.shelp);
    	mainMenuBar.add(fileMenu);
        mainMenuBar.add(kbMenu);
        mainMenuBar.add(queryMenu);
        mainMenuBar.add(helpMenu);
        //
        fileMenuLoad = new JMenu(this.sfileload);
        fileMenuLoadKB = new JMenuItem(sfileloadkb, kbfile);
        fileMenuLoadQuery = new JMenuItem(sfileloadquery, qfile);
        fileMenuLoad.add(fileMenuLoadKB);
        fileMenuLoad.add(fileMenuLoadQuery);
        fileMenuExit = new JMenuItem(this.sexit, exiticon);
        fileMenu.add(fileMenuLoad);
        fileMenu.addSeparator();
        fileMenu.add(fileMenuExit);
        fileMenuExit.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) { System.exit(0); } 
            });
        fileMenuLoadKB.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	fileMenuLoadKB_actionPerformed(e);
            }
        });
        fileMenuLoadQuery.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
            	fileMenuLoadQuery_actionPerformed(e);
            }
        }
        		);
        
        //
        kbMenuSat = new JMenuItem(skbsat, sat);
        kbMenuReasoner = new JCheckBoxMenuItem(skbresoner, reasoner); 
        kbMenuReasoner.setSelected(true);
        kbMenu.add(kbMenuSat);
        kbMenu.addSeparator();
        kbMenu.add(kbMenuReasoner); 
        //
        queryMenuCreate = new JMenuItem(squerycreate, writequery);
        queryMenuEval = new JMenuItem(squeryeval, eval);
        queryMenu.add(queryMenuCreate);
        queryMenu.add(queryMenuEval);
        //
        helpMenuSyntax = new JMenuItem(shelpsyntax, syntaxguide);
        helpMenuSyntax.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		String ops[] ={};
        		JOptionPane.showOptionDialog(
        				null,
            			new TextLoader(false).loadTextToJPane(syntaxFilePath),
            			syntaxFileTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE
                        , null, ops, null);
        	}
        });
        helpMenuConfig = new JMenuItem(shelpconf, config);
        helpMenuAbout = new JMenuItem(sabout, about);
        helpMenu.add(helpMenuConfig);
        helpMenu.add(this.helpMenuSyntax);
        helpMenu.addSeparator();
        helpMenu.add(helpMenuAbout);
        helpMenuAbout.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	aboutmenu_actionPerformed(e);
            }
        });
    }
    
    private void fileMenuLoadKB_actionPerformed(ActionEvent e) {
    	JFileChooser fileChooser = 
    	    	new JFileChooser("");
        fileChooser.setDialogTitle("Select the KB file ...");
        fileChooser.setApproveButtonText("Confirm");
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.addChoosableFileFilter(new KBFileFilter(false));
        fileChooser.showOpenDialog(contentPane);
        File kbFile = fileChooser.getSelectedFile();
        if (kbFile != null) {
            this.loadFile(kbFile);
        }
    }

    private void fileMenuLoadQuery_actionPerformed(ActionEvent e) {
    	JFileChooser fileChooser = new JFileChooser("");
    	fileChooser.setDialogTitle("Select the Query file ...");
    	fileChooser.setApproveButtonText("Confirm");
    	fileChooser.setAcceptAllFileFilterUsed(false);
    	fileChooser.addChoosableFileFilter(new KBFileFilter(true));
        fileChooser.showOpenDialog(contentPane);
        File queryFile = fileChooser.getSelectedFile();
        if (queryFile != null) {
            this.loadFile(queryFile);
        }	
	}

	private void jbInit() throws Exception {
    	//contentPane.setLayout(new GridLayout(1, 2));
        this.setSize(850, 700);
    	this.initMenus();
        this.getContentPane().setLayout(null);
        this.getContentPane().setBackground(java.awt.SystemColor.info);     
        jListClasses.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setBounds(new Rectangle(10, 37, 189, 512));
        jLabel1.setText("Concepts?");
        jLabel1.setBounds(new Rectangle(11, 13, 108, 21));
        jButton1.setBounds(new Rectangle(10, 571, 187, 23));
        jButton1.setText("Consistency?");
        //jButton1.addActionListener(new SystemFrame_jButton1_actionAdapter(this));
        tabs.setBounds(new Rectangle(225, 37, 825, 556));
        jScrollPane2.setAutoscrolls(true);
        //jLabel2.setText(MainClass.LABELS.getProperty("graph.property.label"));
        //jLabel2.setBounds(new Rectangle(225, 13, 109, 18));

        
        contentPane.add(jScrollPane1);
        contentPane.add(jLabel1);
        contentPane.add(jScrollPane2);       
        contentPane.add(jButton1);
        //contentPane.add(jLabel2);
        contentPane.add(tabs);
        jScrollPane1.getViewport().add(jListClasses);
        planScroll.getViewport().add(planPane);
        planPane.setEditable(false);
        planScroll.setAutoscrolls(true);
        gciScroll.getViewport().add(gciPane);
        gciPane.setEditable(false);
        gciScroll.setAutoscrolls(true);
        tabs.add(planScroll);
        tabs.add(jScrollPane2);
        tabs.add(gciScroll);
        
        tabs.setTitleAt(tabs.indexOfComponent(jScrollPane2),"Information");
        tabs.setTitleAt(tabs.indexOfComponent(planScroll),"Query Evaluation Details");
        tabs.setTitleAt(tabs.indexOfComponent(gciScroll),"Knolwedge Base");
    }




    
    public void aboutmenu_actionPerformed(ActionEvent e) {
    	String todisplay = this.frameTitle + 
    			"\n              Author: Jiewen Wu (j55wu@uwaterloo.ca)" +
    			"\n                 Version: 0.1, Date: March 2011";
    	String ops[] ={};
    	JOptionPane.showOptionDialog(
    			contentPane, 
    			todisplay,
                "About the system", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE
                , null, ops, null);
    }

    

    
    private void reasonOnt(){
    

    }
     
    
    
    private void loadFile(File file) {
        reasonOnt();        
    }

    public void jButton1_actionPerformed(ActionEvent e) {
        Object sel = jListClasses.getSelectedValue();
        if (sel == null) {
            JOptionPane.showMessageDialog(this,
                                          "You have to select a concept before pressing this button",
                                          "Attention!",
                                          JOptionPane.WARNING_MESSAGE);
            return;
        }
        try {         

          
            if (true) {
                JOptionPane.showMessageDialog(this, "Some string1 ", "Attention!",
                                              JOptionPane.WARNING_MESSAGE);
            } 
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }




}



